import api from '@/http/api'
export default {
  state: {
    menu: [],
    tabs: [],
    activeItem: '',
    menuRouteLoaded:false
  },
  getters: {
    oneMenu(state) {
      let menus1 = state.menu.filter(function(item){
        return  item.children.length === 0
      })
      return Array.from(new Set(menus1))
    },
    moreMenu(state) {

      let menus2 = state.menu.filter(function(item){
        return item.children.length > 0
      })
      return Array.from(new Set(menus2))

    }
  },
  mutations: {
    intMenu(state, menu) {
      state.menu = menu;
    },
    initTabs(state, tabs) {
      state.tabs = tabs;
    },
    addTab(state, tab) {
      state.tabs.push(tab)
    },
    switchTab(state, nowIndex) {
      state.activeItem = nowIndex
    },
    menuRouteLoaded(state, menuRouteLoaded){  // 改变菜单和路由的加载状态
      state.menuRouteLoaded = menuRouteLoaded;
  }
  },
  actions: {
    getMenu(context) {
      let userName = sessionStorage.getItem('user')
      api.menu.findNavTree({
        'userName': userName
      })
      .then(res => {
        // 保存加载状态
        context.commit('menuRouteLoaded', true)
        // 保存菜单树
        context.commit('intMenu', res.data)

      }).then(res => {
        api.user.findPermissions({
          'name': userName
        }).then(res => {
          // 保存用户权限标识集合
          context.commit('setPerms', res.data)
        })
      })
      .catch(function (res) {
        console.log("获取菜单失败!");
      });
  },
    clickMenuItem(context, index) {
      index=parseInt(index)
      if (index != '11') {
        var tab = context.state.tabs.find(f => f.index == index)
        if (!tab) {
          let menu = null
          context.state.menu.forEach(item => {
            if(item.id==index)
            {
              menu=item
              return false;
            }
            if(item.children.length>0)
            {
              let has=false
              item.children.forEach(child=>{
                if(child.id==index)
                {
                  menu=child
                  has=true
                  return false
                }
              })
              if(has)
              {
                return false
              }
            }
          })

          let url = 'pages'+(menu.url ? menu.url : '/404/404.vue')
          var newTab = {
            label: menu.name,
            index: menu.id,
            closable: true,
            component: url
          }
          context.commit('addTab', newTab)
          console.log(context.state.tabs)
        }

      }
      context.commit('switchTab', String(index))
    },
    closeTab(context, index) {
      index=parseInt(index)

      let activeItem = context.state.activeItem;
      let newTabs = context.state.tabs.filter(f => f.index != index);
      context.commit('initTabs', newTabs)
      let indexNum=newTabs.length
      if (activeItem == index) {
        context.commit('switchTab', indexNum == 0 ? '11' : String(newTabs[newTabs.length-1].index))
      }
    }
  }
}
